package org.yanghaokun.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yanghaokun.basic.util.AjaxResult;
import org.yanghaokun.basic.util.LoginContext;
import org.yanghaokun.basic.util.PageList;
import org.yanghaokun.order.domain.AdoptOrder;
import org.yanghaokun.order.query.AdoptOrderQuery;
import org.yanghaokun.order.service.IAdoptOrderService;
import org.yanghaokun.user.domain.LoginInfo;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){

        try{
            String payData = adoptOrderService.submit(params, LoginContext.getLoginInfo(request));
            return AjaxResult.me().setResultObj(payData);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("领养失败！"+e.getMessage());
        }

    }

    //管理员查询接口，频台管理员可以查询全部，店铺管理员或这员工值可以查询自己的店铺
    @PostMapping("/admin")
    public PageList<AdoptOrder> queryAdminOrder(@RequestBody AdoptOrderQuery adoptOrderQuery,HttpServletRequest request) {
        //根据登陆信息查找登录的用户，判断使频台管理员还是店铺管理员还是员工
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return adoptOrderService.queryAdminOrder(adoptOrderQuery,loginInfo);
    }

    //用户查询接口
    @PostMapping("/user")
    public PageList<AdoptOrder> queryUserOrder(@RequestBody AdoptOrderQuery adoptOrderQuery,HttpServletRequest request) {
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        return adoptOrderService.queryUserOrder(adoptOrderQuery,loginInfo);
    }
}